#include "ClientTest.h"
#include <unistd.h>
#include <string.h>
#include <iostream>
#include <thread>

void ClientTest::Run(std::string ip,int port)
{
    conn = std::unique_ptr<ClientConnect>(new ClientConnect());
    conn->Connect(ip,port);
    int i = 0;
    int old = 0;
    bool flag = false;
    while(1)
    {

        if(!conn->Statue())
        {
            std::this_thread::sleep_for(std::chrono::milliseconds(30));
            continue;
        }
        conn->Update();
        char buf[1024];
        if(i == 0 || i != old || !flag )
        {
            int n = sprintf(buf,"hello worldhello worldhello worldhello worldhello worldhellhello worldhello worldhello worldhello worldhello worldhellhello worldhello worldhello worldhello worldhello worldhellhello worldhello worldhello worldhello worldhello worldhellhello worldhello worldhello worldhello worldhello worldhellhello worldhello worldhello worldhello worldhello worldhellhello worldhello worldhello worldhello worldhello worldhellhello worldhello worldhello worldhello worldhello worldhellhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello world  id=%d",i);
            conn->Send(buf,n);
            {
                auto start  = std::chrono::high_resolution_clock::now();
            //        std::cout<<"start i="<<i<<" time:"<<std::chrono::system_clock::to_time_t(start)<<std::endl;
                maps.emplace(i,start);
            }
            old = i;
            flag = true;
        }

        memset(buf,0,sizeof(buf));
        int n = 0;
        do
        {
            n = conn->Recv(buf,1024);
            if(n > 0)
            {
                int id = 0;
                sscanf(buf,"hello worldhello worldhello worldhello worldhello worldhellhello worldhello worldhello worldhello worldhello worldhellhello worldhello worldhello worldhello worldhello worldhellhello worldhello worldhello worldhello worldhello worldhellhello worldhello worldhello worldhello worldhello worldhellhello worldhello worldhello worldhello worldhello worldhellhello worldhello worldhello worldhello worldhello worldhellhello worldhello worldhello worldhello worldhello worldhellhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello world  id=%d",&id);
                auto it = maps.find(id);
                auto end = std::chrono::high_resolution_clock::now();
                std::chrono::duration<double> diff = end-it->second;
                std::cout<<"recv="<<n<<"\t id="<<it->first<<" begin="<< std::chrono::system_clock::to_time_t(it->second)
                    <<"\tbuf:"<<buf<<"\tid="<<id<<"\ttime:"<<diff.count()<<std::endl;
                i++;
            }
        }while(n> 0);
        std::this_thread::sleep_for(std::chrono::milliseconds(10));
    }
}

